package com.boyu.backend;

import com.boyu.common.BaseController;
import com.boyu.common.OptionConst;
import com.boyu.common.SystemConst;
import com.boyu.common.model.Option;
import com.boyu.common.model.Product;
import com.boyu.common.model.ProductAttr;
import com.boyu.common.model.ProductImage;
import com.jfinal.upload.UploadFile;

import java.util.List;

/**
 * _ProductImageController
 */
public class _ProductImageController extends BaseController {
	protected int getGroupId() {
		return OptionConst.BIKE_ATTR_GROUP;
	}

	public void index() {
		Object productId = getPara("productId");
		if (productId == null) {
			productId = getAttr("productId");
		}


		List<ProductImage> productImageList = ProductImage.dao.find("select * from t_product_image where isDel=0 and productId = " + productId);
		Product product = Product.dao.findFirst("select * from t_product where id=" + productId);
		setAttr("productImageList", productImageList);
		setAttr("product", product);
		render(INDEX_HTML);
	}

	public void add() {
		String productId = getPara("productId");
		Product product = Product.dao.findFirst("select * from t_product where id=" + productId);
		setAttr("product", product);
		render(ADD_HTML);
	}

	public void edit() {
		List<Option> options = Option.dao.find("select * from t_option where groupId = " + OptionConst.BIKE_ATTR_GROUP);
		setAttr("optionList", options);
		long id = getParaToLong(ID);
		ProductAttr byId = ProductAttr.dao.findById(id);
		setAttr("model", byId);
		setAttr("optionId", byId.getOptionId());
		render(EDIT_HTML);
	}

	public void save() {
		UploadFile fileName = getFile("fileName", SystemConst.UPLOAD_PRODUCT);
		String productId = getPara("productId");
		ProductImage model = getModel(ProductImage.class, "");
		model.setImage(SystemConst.UPLOAD_PRODUCT+"/"+fileName.getFileName());
		if (model.getId() != null) {
			model.update();
		} else {
			model.save();
		}
		setAttr("productId",productId);
		index();
	}

	public void del() {
		ProductImage byId = ProductImage.dao.findById(getParId());
		byId.setIsDel(1);
		byId.update();
		setAttr("productId",byId.getProductId());
		index();
	}
}





